//
//  DAO.m
//  MusouKit
//
//  Created by danal on 13-4-24.
//  Copyright (c) 2013年 danal. All rights reserved.
//

#import "DAO.h"

@implementation DAO

- (void)dealloc{
    [_dataDict release];    _dataDict = nil;
    [super dealloc];
}

- (id)init{
    self = [super init];
    if (self) {
        _dataDict = [[NSMutableDictionary alloc] init];
    }
    return self;
}

#pragma mark - Respond to property methods

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
    NSString *sel = NSStringFromSelector(aSelector);
    if ([sel rangeOfString:@"set"].location == 0) {
        
        return [NSMethodSignature signatureWithObjCTypes:"v@:@"];
    } else {
        
        return [NSMethodSignature signatureWithObjCTypes:"@@:"];
    }
}

- (void)forwardInvocation:(NSInvocation *)anInvocation{
    NSString *key = NSStringFromSelector([anInvocation selector]);
    if ([key rangeOfString:@"set"].location == 0) {     //setter
        
        key = [[key substringWithRange:NSMakeRange(3, [key length] - 4)] lowercaseString];
        NSString *obj;
        [anInvocation getArgument:&obj atIndex:2];      //0-self; 1-_cmd
        [self saveValue:obj forKey:key];
//        [_dataDict setObject:obj forKey:key];
        
    } else {    //getter
        
//        NSString *obj = [_dataDict objectForKey:key];
        id obj = [self readValueForKey:key];
        [anInvocation setReturnValue:&obj];
        
    }
    
}

- (void)saveValue:(id)value forKey:(NSString *)key{
        [_dataDict setObject:value forKey:key];
}

- (id)readValueForKey:(NSString *)key{
    NSString *obj = [_dataDict objectForKey:key];
    return obj;
}

@end
